package com.lemon.smartsafety.ui.fragment.fire;

import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.HttpBack;
import com.lemon.smartsafety.http.HttpOkBase;
import com.lemon.smartsafety.http.HttpPath;
import com.lemon.smartsafety.result.BaseMode;
import com.lemon.smartsafety.result.fire.SynthesizeDetail;
import com.lemon.smartsafetynew.ui.activity.base.NewBaseFragment;
import com.lemon.smartsafety.utils.CommonUtil;
import com.lemon.smartsafety.utils.JsonUtils;
import com.lemon.smartsafety.utils.app.ToastUtils;
import com.lemon.smartsafety.view.TasksCompletedView;
import com.lemon.smartsafety.view.ViewRadarTitle;
import com.lemon.smartsafety.view.dialog.DialogRadarView;
import com.lemon.smartsafety.view.radar.RadarView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/10 15:27
 * 修改人：Fargo
 * 修改时间：2016/8/10 15:27
 * 修改备注：
 */
public class FirefightingOverallFragment extends NewBaseFragment {
    @Bind(R.id.tasks_view)
    TasksCompletedView tasksView;
    @Bind(R.id.txt_num1)
    TextView txtNum1;
    @Bind(R.id.txt_num2)
    TextView txtNum2;
    @Bind(R.id.txt_num3)
    TextView txtNum3;
    @Bind(R.id.txt_num4)
    TextView txtNum4;
    @Bind(R.id.radarView)
    RadarView radarView;
    @Bind(R.id.txt_title)
    TextView txtTitle;
    @Bind(R.id.txt_companyNum1)
    TextView txtCompanyNum1;
    @Bind(R.id.txt_companyNum2)
    TextView txtCompanyNum2;
    @Bind(R.id.viewRadarTitle)
    ViewRadarTitle viewRadarTitle;

    private CompanyInfo mCompanyInfo;
    public SynthesizeDetail synthesizeDetail;

    private String[] titles;
    private double[] data1;
    private double[] data2;

    private DialogRadarView dialogRadarView;
    private String supervise_code="";


    @Override
    protected void getBundleArgs() {
        if (getArguments().containsKey(BundleKey.COMPANY_INFO)) {
            mCompanyInfo = (CompanyInfo) getArguments().getSerializable(BundleKey.COMPANY_INFO);
        }
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_firefighting_overall;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initHttp() {
        initAllHttp();
    }

    @Override
    protected void initList() {

    }

    @Override
    protected void loadList() {

    }

    @Override
    protected void refreshView() {
        txtTitle.setText(synthesizeDetail.getSuperviseName());
        txtCompanyNum1.setText(String.valueOf(synthesizeDetail.getSmallRangeCompanyNum()));
        txtCompanyNum2.setText("/" + String.valueOf(synthesizeDetail.getWideRangeCompanyNum()));

        int progress = 1;
        try {
            progress = synthesizeDetail.getSmallRangeCompanyNum() * 100 / synthesizeDetail.getWideRangeCompanyNum();
            if(progress<1){
                progress=1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        tasksView.setProgress(progress);

        titles = new String[synthesizeDetail.getWideRangeScore().size()];
        data1 = new double[synthesizeDetail.getWideRangeScore().size()];
        data2 = new double[synthesizeDetail.getWideRangeScore().size()];

        for (int i = 0; i < synthesizeDetail.getSmallRangeScore().size(); i++) {
            titles[i] = synthesizeDetail.getSmallRangeScore().get(i).getTargetName();
            data1[i] = synthesizeDetail.getSmallRangeScore().get(i).getAvgScore();
            try {
                if (synthesizeDetail.getSmallRangeScore().get(i).getAvgScore()==0) {
                    data1[i] = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        for (int i = 0; i < synthesizeDetail.getWideRangeScore().size(); i++) {
            data2[i] = synthesizeDetail.getWideRangeScore().get(i).getAvgScore();
            try {
                if (synthesizeDetail.getWideRangeScore().get(i).getAvgScore()==0) {
                    data2[i] = 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        radarView.setTitles(titles);
        radarView.setDataOne(data1);
        radarView.setDataTwo(data2);
        radarView.setCount(titles.length);
        radarView.resetShow();

        for (int i = 0; i < synthesizeDetail.getSuperviseNum().size(); i++) {
            switch (i) {
                case 0:
                    txtNum1.setText(synthesizeDetail.getSuperviseNum().get(i).getSuperviseName() + " " + synthesizeDetail.getSuperviseNum().get(i).getCompanyNum());
                    txtNum1.setVisibility(View.VISIBLE);
                    break;
                case 1:
                    txtNum2.setText(synthesizeDetail.getSuperviseNum().get(i).getSuperviseName() + " " + synthesizeDetail.getSuperviseNum().get(i).getCompanyNum());
                    txtNum2.setVisibility(View.VISIBLE);
                    break;
                case 2:
                    txtNum3.setText(synthesizeDetail.getSuperviseNum().get(i).getSuperviseName() + " " + synthesizeDetail.getSuperviseNum().get(i).getCompanyNum());
                    txtNum3.setVisibility(View.VISIBLE);
                    break;
                case 3:
                    txtNum4.setText(synthesizeDetail.getSuperviseNum().get(i).getSuperviseName() + " " + synthesizeDetail.getSuperviseNum().get(i).getCompanyNum());
                    txtNum4.setVisibility(View.VISIBLE);
                    break;
            }
        }

        viewRadarTitle.setVisibility(View.VISIBLE);
        viewRadarTitle.setTitle1(synthesizeDetail.getSuperviseName()+":");
        viewRadarTitle.setTitleImg1(ContextCompat.getColor(mActivity, R.color.txt_color2));

        viewRadarTitle.setTitle2("全市本类型单位:");
        viewRadarTitle.setTitleImg2(ContextCompat.getColor(mActivity, R.color.txt_color3));

    }


    @OnClick({R.id.radarView,R.id.txt_num1,R.id.txt_num2,R.id.txt_num3,R.id.txt_num4})
    public void onButtonClick(View v) {
        switch (v.getId()) {
            case R.id.radarView:
                if(CommonUtil.objIsEmpty(synthesizeDetail)){
                    return;
                }

                if (dialogRadarView == null) {
                    dialogRadarView = new DialogRadarView(mActivity);
                }
                if (titles != null) {
                    dialogRadarView.setTitles(titles);
                }
                if (data1 != null) {
                    dialogRadarView.setData1(data1);
                }
                if (data2 != null) {
                    dialogRadarView.setData2(data2);
                }
                dialogRadarView.setStrTitle1(synthesizeDetail.getSuperviseName()+":");
                dialogRadarView.setStrTitle2("全市本类型单位:");
                dialogRadarView.init(mActivity);
                break;
            case R.id.txt_num1:
                try {
                    supervise_code=synthesizeDetail.getSuperviseNum().get(0).getSuperviseCode();
                    initAllHttp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.txt_num2:
                try {
                    supervise_code=synthesizeDetail.getSuperviseNum().get(1).getSuperviseCode();
                    initAllHttp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.txt_num3:
                try {
                    supervise_code=synthesizeDetail.getSuperviseNum().get(2).getSuperviseCode();
                    initAllHttp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.txt_num4:
                try {
                    supervise_code=synthesizeDetail.getSuperviseNum().get(3).getSuperviseCode();
                    initAllHttp();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
    }


    @Override
    protected void callFinish() {

    }


    /**
     * 初始化顶部
     */
    public void initAllHttp() {
        LemonRequestParams params = new LemonRequestParams("application_user_bind", "bind_id", "");
        params.put("check_type", "1");
        params.put("supervise_code",supervise_code);

        mHUDUtil.show();
        HttpOkBase.httpBasePost(params, HttpPath.getmBase(HttpPath.Get_synthesize_detail), "", new HttpOkBase.HttpInterface() {
            @Override
            public void callBackFunction(boolean httpClient, String result) {

                mHUDUtil.hidden();
                if (httpClient) {
                    BaseMode baseMode = JsonUtils.fromJson(result, BaseMode.class);
                    if (!CommonUtil.objIsEmpty(baseMode)) {
                        if (HttpBack.httpBackError(baseMode)) {

                            try {
                                synthesizeDetail = mGson.fromJson(mGson.toJson(baseMode.getResult()), SynthesizeDetail.class);
                                refreshView();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        } else {
                            ToastUtils.toastMsg(baseMode.getMsg());
                        }
                    }
                }
            }
        });

    }



}